﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.GJIBZ;
using StateGrid.GlobalVariables;

public partial class AddFL : System.Web.UI.Page
{
    private GJIBZ_DAO gjibzDAO = new GJIBZ_DAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        if (!this.IsPostBack)
        {
            this.BindData();
        }  
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void BindData()
    {
        //获取分类编码
        this.flbmHiddenField.Value = (String)Request["parent"];
        if (null == this.flbmHiddenField.Value || "".Equals(this.flbmHiddenField.Value))
        {
            String message = "没有获取到上级分类编码";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }
        this.flbmTextBox.Text = this.flbmHiddenField.Value;
    }

    protected void submitButton_Click(object sender, EventArgs e)
    {
        //清空Label信息
        this.ClearLabel();
        //验证分类名称是否有效
        if (!ValidateFLMC())
        {
            return;
        }
        //验证分类编码是否有效
        if (!ValidateFLBM())
        {
            return;
        }
        //验证通过则插入新记录
        GJIBZ_FL gjibzFL = new GJIBZ_FL();
        gjibzFL.flmc = this.flmcTextBox.Text.Trim();
        gjibzFL.flbm = this.flbmTextBox.Text.Trim();
        gjibzFL.zyfl = this.zyflTextBox.Text.Trim();
        gjibzFL.ywmc = this.wwmcTextBox.Text.Trim();
        gjibzFL.wjtz = this.wjtzTextBox.Text.Trim();
        gjibzFL.fjtz = this.fjtzTextBox.Text.Trim();

        gjibzDAO.InsertGJIBZ_FL(gjibzFL);

        Response.Write("<Script>alert('添加国际标准分类成功')</Script>");
    }

    /// <summary>
    /// 清空Label信息
    /// </summary>
    private void ClearLabel()
    {
        this.errorFLBMLabel.Text = "";
        this.errorFLMCLabel.Text = "";
    }

    /// <summary>
    /// 验证分类编码
    /// </summary>
    private Boolean ValidateFLBM()
    {
        Boolean flag = true;
        this.errorFLBMLabel.Text = "";
        if (this.flbmTextBox.Text.Trim().Length != this.flbmHiddenField.Value.Length + 2)
        {
            this.errorFLBMLabel.Text = "错误：分类编码位数不正确";
            flag = false;
        }
        else
        {
            if (!this.flbmTextBox.Text.StartsWith(this.flbmHiddenField.Value))
            {
                this.errorFLBMLabel.Text = "错误：分类编码不正确";
                flag = false;
            }
            else
            {
                GJIBZ_FL gjibzFL = this.gjibzDAO.GetGJIBZ_FLByFlbm(this.flbmTextBox.Text.Trim());
                if (null != gjibzFL)
                {
                    this.errorFLBMLabel.Text = "错误：该分类编码已存在";
                    flag = false;
                }
            }
        }

        return flag;
    }

    /// <summary>
    /// 验证分类名称
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateFLMC()
    {
        Boolean flag = true;
        this.errorFLMCLabel.Text = "";

        if (null == this.flmcTextBox.Text || "".Equals(this.flmcTextBox.Text.Trim()))
        {
            this.errorFLMCLabel.Text = "错误：分类名称不能为空";
            flag = false;
        }

        GJIBZ_FL gjibzFL = gjibzDAO.GetGJIBZ_FLByFlmc(this.flmcTextBox.Text.Trim());

        if (null != gjibzFL)
        {
            this.errorFLMCLabel.Text = "错误：分类名称已存在";
            flag = false;
        }
        return flag;
    }
    
    /// <summary>
    /// 恢复分类编码初始信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.flbmTextBox.Text = this.flbmHiddenField.Value;
    }
    protected void wjtzTextBox_TextChanged(object sender, EventArgs e)
    {
        this.wjtzTextBox.Text = GlobalMethod.Semicolon(this.wjtzTextBox.Text.Trim());
    }
}